﻿using System;
using System.Xml;
using OpenQA.Selenium;
using Ro.AssistFunction.BackNodeInfo;
using Ro.AssistFunction.OutputManage;
using Ro.Common.RoArgs;
using Ro.Common.RoInterface.UserType;
using Ro.DriverEvent.FindElement;

namespace Ro.DriverEvent.IntegrationFun
{
    /// <summary>
    /// 构造函数
    /// IsTure和IsFalse本质是相同的，仅在处理结果进行了不同分拆
    /// </summary>
    public class WebIs
    {
        /// <summary>
        /// 返回值，返回执行结果
        /// todo 3-27更新成WebActionResult
        /// </summary>
        public WebActionResult BackResult;

        /// <summary>
        /// 构造函数实体
        /// 进行执行
        /// </summary>
        /// <param name="sigNode">IsTrue或IsFalse节点</param>
        public WebIs(XmlNode sigNode)
        {
            //返回值
            WebActionResult back = new WebActionResult();

            ExtractNeedInfo extractNeedInfo = new ExtractNeedInfo();
            //提取超时
            TimeSpan timeout = extractNeedInfo.ExtractTimeout(sigNode);

            //实际类型
            string acttype = extractNeedInfo.ExtractAttributeValue(sigNode.FirstChild, "xsi:type");
            //提取元素
            string value = extractNeedInfo.ExtractAttributeValue(sigNode.FirstChild, "web:RoWebElementID");

            //构造函数初始化，获取元素
            FindWebElement findElement = new FindWebElement(value, timeout);
            IWebElement ele = findElement.WebElement;

            try
            {
                //如果元素为空，反馈结果为空
                if (ele != null)
                {
                    //初始化实际使用的实际值
                    bool realactvalue;

                    //循环实际值类型
                    switch (acttype)
                    {
                        case "web:Browser.IsPageLoaded":
                            IJavaScriptExecutor jsExecutor = RoTestObject.WebTestObject as IJavaScriptExecutor;
                            string com = (string) jsExecutor?.ExecuteScript("return document.readyState");
                            //写入实际值
                            realactvalue = com == "complete";
                            back = BoolResOut(realactvalue, sigNode.Name, value);
                            break;

                        case "web:RoWebElement.Displayed":
                            realactvalue = ele.Displayed;
                            back = BoolResOut(realactvalue, sigNode.Name, value);
                            break;

                        case "web:RoWebElement.Enabled":
                            realactvalue = ele.Enabled;
                            back = BoolResOut(realactvalue, sigNode.Name, value);
                            break;

                        case "web:RoWebElement.Selected":
                            realactvalue = ele.Selected;
                            back = BoolResOut(realactvalue, sigNode.Name, value);
                            break;
                    }
                }
                else
                {
                    back = BackActionResult.BackWebResult(false, value, "WebIs操作遭遇查找元素失败，获取元素为null"); //结果
                }
            }
            // 抛出异常

            catch (Exception e)
            {
                back = BackActionResult.BackWebResult(false, value, "验证True/False操作遭遇System异常"); //结果
                ExceptionOutPut.SystemExceptionOut("WebEqual", e);
            }
            //返回值
            BackResult = back;
        }


        /// <summary>
        /// 针对bool属性判断的输出语句
        /// </summary>
        /// <param name="realactvalue">实际真实值</param>
        /// <param name="typename">euqal or not euqal</param>
        /// <param name="value">控件名称</param>
        private WebActionResult BoolResOut(bool realactvalue, string typename, string value)
        {
            WebActionResult back = new WebActionResult();

            if (typename == "web:WaitUntil.IsTrue")
            {
                if (realactvalue)
                {
                    back = BackActionResult.BackWebResult(true, value, "验证值等/不等验证成功，预期值：True" + " ,实际值：" + true); //结果
                }
                else
                {
                    back = BackActionResult.BackWebResult(false, value, "验证值等/不等验证失败，预期值：True" + " ,实际值：" + false); //结果
                }
            }
            else
            {
                if (!realactvalue)
                {
                    back = BackActionResult.BackWebResult(true, value, "验证值等/不等验证成功，预期值：False" + " ,实际值：" + false); //结果
                }
                else
                {
                    back = BackActionResult.BackWebResult(false, value, "验证值等/不等验证失败，预期值：False" + " ,实际值：" + true); //结果
                }
            }
            return back;
        }
    }
}

